<?php

namespace app\common\model;

use think\Request;
use think\Model;

class FormworkOrder extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 定义字段类型
    protected $type = [
    ];
    protected $append = [
        'type'
    ];

    /**
     * 门店类型
     */
    public function getTypeAttr($value,$data)
    {
        $status = [10 => '单门店', 20 => '多门店'];
        return $status[$data['shop_type']];
    }

    /**
     * 关联模板表
     */
    public function formwork()
    {
        return $this->belongsTo('app\\common\\model\\Formwork','formwork_id','id');
    }

    /**
     * 关联小程序表
     */
    public function applet()
    {
        return $this->belongsTo('app\\common\\model\\Applet','applet_id','id');
    }

    /**
     * 获取列表
     */
    public function getList($user_id=0)
    {
        // 筛选条件
        $filter = [];
        $user_id > 0 && $filter['user_id'] = $user_id;
        // 执行查询
        $list = $this->with(['formwork','formwork.version'])
            ->where($filter)
            ->order('id','desc')
            ->paginate(15, false, [
                'query' => Request::instance()->request()
            ]);
        return $list;
    }

    /**
     * 获取详情
     */
    public static function detail($id)
    {
        return self::get($id, ['formwork','formwork.version']);
    }
}
